package com.xqboss.web.controller.equipment;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.xqboss.apps.domain.equipment.EquipmentUpgradeTemplate;
import com.xqboss.apps.dto.PageDto;
import com.xqboss.apps.dto.equipment.EquipmentUpgradeHoldDto;
import com.xqboss.apps.dto.equipment.EquipmentUpgradeTemplateDto;
import com.xqboss.apps.dto.equipment.WebEquipmentPreUpgradeRecordDto;
import com.xqboss.apps.service.equipment.EquipmentUpgradeRecordService;
import com.xqboss.apps.service.equipment.EquipmentUpgradeTemplateService;
import com.xqboss.apps.vo.equipment.EquipmentUpgradeTemplateVo;
import com.xqboss.apps.vo.equipment.WebEquipmentUpgradeRecordVo;
import com.xqboss.common.annotation.Log;
import com.xqboss.common.annotation.RepeatSubmit;
import com.xqboss.common.core.controller.BaseController;
import com.xqboss.common.core.domain.R;
import com.xqboss.common.enums.BusinessType;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotNull;
import java.util.List;

/**
 * @author Administrator
 */
@Api(tags = "装备升级加点-模板")
@RestController
@RequestMapping("/web/equipment/upgrade/template")
public class WebEquipmentUpgradeTemplateController extends BaseController {

    @Autowired
    private EquipmentUpgradeTemplateService equipmentUpgradeTemplateService;
    @Autowired
    private EquipmentUpgradeRecordService equipmentUpgradeRecordService;

    @PreAuthorize("@ss.hasPermi('web:equipment:upgrade:template:list')")
    @ApiOperation("列表")
    @GetMapping("/list")
    public R<List<EquipmentUpgradeTemplateVo>> attrList(PageDto params) {
        params.startPageAndOrder();
        // 获取列表根据id倒序
        List<EquipmentUpgradeTemplate> list = equipmentUpgradeTemplateService.list(new LambdaQueryWrapper<EquipmentUpgradeTemplate>()
                .orderByDesc(EquipmentUpgradeTemplate::getId));
        List<EquipmentUpgradeTemplateVo> voList = BeanUtil.copyToList(list, EquipmentUpgradeTemplateVo.class);
        return R.ok(voList, list);
    }


    @RepeatSubmit(interval = 1000, justUrl = true)
    @PreAuthorize("@ss.hasPermi('web:equipment:upgrade:template:add')")
    @ApiOperation("新增/编辑")
    @PostMapping("/add-edit")
    public R<Boolean> addEdit(@Validated @RequestBody EquipmentUpgradeTemplateDto params) {
        return R.ok(equipmentUpgradeTemplateService.add(params));
    }

    @PreAuthorize("@ss.hasPermi('web:equipment:upgrade:template:details')")
    @ApiOperation("详情")
    @GetMapping("/details/{id}")
    public R<EquipmentUpgradeTemplateDto> details(@PathVariable("id")
                                                  @ApiParam(value = "升级模版ID", required = true)
                                                  @NotNull(message = "请输入升级模版ID")
                                                  Long id) {
        return R.ok(equipmentUpgradeTemplateService.details(id));
    }

    @PreAuthorize("@ss.hasPermi('web:equipment:upgrade:template:detect-hold')")
    @ApiOperation("检测持仓/模式开关")
    @PutMapping("/update-detect-hold")
    @Log(title = "检测持仓/模式开关", businessType = BusinessType.UPDATE)
    public R<Boolean> updateSaleable(@Validated @RequestBody EquipmentUpgradeHoldDto params) {
        return R.ok(equipmentUpgradeTemplateService.updateDetectHold(params));
    }

    @PreAuthorize("@ss.hasPermi('web:equipment:upgrade:template:del-config')")
    @ApiOperation("删除配置")
    @DeleteMapping("/del-config/{ids}")
    public R<Boolean> delConfig(@PathVariable("ids")
                                @ApiParam(value = "配置ID", required = true)
                                @NotNull(message = "请输入升级配置ID")
                                Long[] ids) {
        return R.ok(equipmentUpgradeTemplateService.delConfig(ids));
    }

    @PreAuthorize("@ss.hasPermi('web:equipment:upgrade:template:del-config-coefficient')")
    @ApiOperation("删除配置(系数)")
    @DeleteMapping("/del-config-coefficient/{ids}/{type}")
    public R<Boolean> delConfigCoefficient(@PathVariable("ids")
                                           @ApiParam(value = "配置系数ID", required = true)
                                           @NotNull(message = "请输入升级配置系数ID")
                                           Long[] ids,
                                           @PathVariable("type")
                                           @ApiParam(value = "0删除配置系数 1删除配置突破材料")
                                           int type) {
        return R.ok(equipmentUpgradeTemplateService.delConfigCoefficient(ids, type));
    }

    @PreAuthorize("@ss.hasPermi('web:equipment:upgrade:record:list')")
    @ApiOperation("升级记录")
    @PostMapping("/upgrade-record")
    public R<List<WebEquipmentUpgradeRecordVo>> upgradeRecord(@RequestBody @Validated WebEquipmentPreUpgradeRecordDto params) {
        params.startPage();
        return R.ok(equipmentUpgradeRecordService.upgradeRecord(params));
    }


}
